package org.goobs.mapping;

import java.util.HashSet;
import java.util.Set;

public class City {
	
	private boolean isJunction;
	private double longitude;
	private double latitude;
	private String name;
	private int population;
	private Set <IntercityRoad> roads = new HashSet <IntercityRoad> ();
	
	public City(double longitude, double latitude, String name, int population){
		this.longitude = longitude;
		this.latitude = latitude;
		this.name = name;
		this.population = population;
	}
	
	public String getName(){
		return name;
	}
	
	public int getPopulation(){
		return population;
	}
	
	public double getLongitude(){
		return longitude;
	}
	
	public double getLatitude(){
		return latitude;
	}
	
	public double radius(){
		if(population < 0){
			return 10.0;
		}else{
			return Math.max(10.0, Math.sqrt(population / 1570.5 ));
		}
	}
	
	public void addRoad(IntercityRoad road){
		this.roads.add(road);
	}
	
	public Set <IntercityRoad> getRoads(){
		return roads;
	}
	
	public void setAsJunction(){
		this.isJunction = true;
	}
	
	public boolean isJunction(){
		return isJunction;
	}
	
	public String toString(){
		StringBuilder b = new StringBuilder();
		b.append(name).append(" (").append(longitude).append(", ").append(latitude).append(") population: ").append(population);
		return b.toString();
	}
	
	
	public static City combine(City a, City b){
		return new City( (a.longitude + b.longitude)/2, (a.latitude + b.latitude)/2, a.name + " / " + b.name, a.population + b.population );
	}
}
